package Huihu.src.Duoxiancheng;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class Practice {
    public static void main(String[] args) {
        //定义一个阻塞队列，队列大小可以装16个信息
        BlockingQueue<String> queue = new ArrayBlockingQueue<String>(16);
        for (int i = 0; i < 4; i ++) {
            //开启四个线程去阻塞队列中取日志打印
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while(true) {
                        try {
                            String log = queue.take();
                            //取日志
                            parseLog(log);
                            //打印日志
                        }
                        catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
            ).start();
        }
        System.out.println("begin:" + (System.currentTimeMillis()/1000));
        for (int i = 0; i < 16; i ++) {
            final String log = "" + (i+1);
            //表示一个日志
            try {
                queue.put(log);
                //将产生的日志塞到阻塞队列中去
            }
            catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    public static void parseLog(String log) {
        //打印日志的方法
        System.out.println(Thread.currentThread().getName() + "---"
                + log + "---" + (System.currentTimeMillis()/1000));
        try {
            Thread.sleep(1000);
            //模拟打印一次日志需要1秒
        }
        catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
